
import 'package:FlutterRoute/common/models/LoginUser.dart';
import 'package:FlutterRoute/common/models/Store.dart';
import 'package:flutter/material.dart';
import 'ui/detail.dart';
import 'ui/login.dart';
import 'ui/baseWidget.dart';
import 'ui/layout.dart';
import 'ui/container.dart';
import 'ui/mydrawer.dart';
import 'ui/baseUI.dart';
import 'func/funcAndAanimate.dart';
import '../widget/ZKTabbarViewItem.dart';
import 'ui/scrollview.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import '../generated/l10n.dart';
import 'package:provider/provider.dart';
import 'func/ProviderVC.dart';

void main() {
  runApp(
    MyApp(),
  );
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return Store.init(context: context,
        child: Builder(builder: (context){
          Store.widgetCtx = context;
          return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              // This is the theme of your application.
              //
              // Try running your application with "flutter run". You'll see the
              // application has a blue toolbar. Then, without quitting the app, try
              // changing the primarySwatch below to Colors.green and then invoke
              // "hot reload" (press "r" in the console where you ran "flutter run",
              // or simply save your changes to "hot reload" in a Flutter IDE).
              // Notice that the counter didn't reset back to zero; the application
              // is not restarted.
              primarySwatch: Colors.red,
              // This makes the visual density adapt to the platform that you run
              // the app on. For desktop platforms, the controls will be smaller and
              // closer together (more dense) than on mobile platforms.
              visualDensity: VisualDensity.adaptivePlatformDensity,
            ),
            initialRoute: "/",
            routes: {
              "detail":(context) => NewRoute(txt: ModalRoute.of(context).settings.arguments,),
              "login":(context) => LoginVC(),
              "widget_state":(context) => BaseWidget(),
              "widget_tapboxA":(context) => TapBoxA(),
              "widget_tapboxB":(context) => TapBoxB(title: ModalRoute.of(context).settings.arguments),
              "TapBoxCListVC":(context) => TapBoxCListVC(),
              "LayoutListVC":(context) => LayoutListVC(),
              "WrapLayoutVC":(context) => WrapLayoutVC(),
              "FlowLayoutVC":(context) => FlowLayoutVC(),
              "ContainerVC":(context) => ContainerVC(),
              "ScrollViewListVC":(context) => ScrollViewListVC(),
              "ListViewVC":(context)=> ListViewVC(),
              "ProviderVC":(context)=> ProviderVC(),
              // "/":(context) => MyApp(),
            },
            home: MyHomePage(title: "Demo",),
            localizationsDelegates: [
              S.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate
            ],
            supportedLocales: S.delegate.supportedLocales,
          );
        })
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      drawer: mydriver(),
      bottomNavigationBar: BottomAppBar(
        color: Colors.white,
        shape: CircularNotchedRectangle(),
        notchMargin: 10,
        child: Row(
          children: [
            ZKTabbarViewItem(selectedIdx: _selectedIndex,idx:0,icon:Icons.build,text:S.of(context).ui_and_layout,onPress: _onTabItemPress,),
            SizedBox(),
            ZKTabbarViewItem(selectedIdx: _selectedIndex,idx:1,icon:Icons.animation,text:"功能与动画",onPress: _onTabItemPress,),
          ],
          mainAxisAlignment: MainAxisAlignment.spaceAround,
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(child: Icon(Icons.add),backgroundColor: Colors.red,onPressed: _tabCenterButtonTouch,),
      // bottomNavigationBar: BottomNavigationBar(
      //   currentIndex: _selectedIndex,
      //   onTap:_tabbarTouchForIndex,
      //   items: [
      //     BottomNavigationBarItem(
      //       icon: Icon(Icons.build),
      //       label:"UI组件与布局"
      //     ),
      //     BottomNavigationBarItem(
      //         icon: Icon(Icons.build),
      //         label:"功能与动画"
      //     )
      //   ],
      // ),
      body: _getBodyWidget(),// This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void _onTabItemPress(idx){
    if(idx == this._selectedIndex){
      return;
    }
    setState(() {
      this._selectedIndex = idx;
    });
  }

  Widget _getBodyWidget(){
    // S.load(Locale('zh', 'CN'));
    switch(_selectedIndex){
      case 0:{
        return baseUI();
      }
      case 1:{
        return FuncAndAnimate();
      }
      default:
        return baseUI();
    }
  }

  void _tabCenterButtonTouch(){
    debugPrint("tap _tabCenterButtonTouch");
  }

  void _tabbarTouchForIndex(idx){
    setState(() {
      _selectedIndex = idx;
    });
  }
}
